Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasi: add getEnvMap #2390

Merged
merged 3 commits into from
Apr 30, 2019
Merged

wasi: add getEnvMap #2390

merged 3 commits into from
Apr 30, 2019

Conversation

shritesh
Copy link
Contributor

@shritesh shritesh commented Apr 30, 2019

See: WebAssembly/WASI#27 for the null pointer todo

std/os.zig Outdated
if (env) |ptr| {
var line_i: usize = 0;
while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {}
if (ptr[line_i] != '=') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be an assertion. If the WASI functions don't do what they're documented to do, that's definitely going to cause undefined behavior. And environ_get is documented to return the environment string with the format key=value, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation doesn't say anything. wasmtime returns it in key=value, wasmer returns argv[0] which is definitely a bug.

Copy link
Member

@andrewrk andrewrk Apr 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 2 choices here:

  1. make the zig code assume the WASI host is correct and do a little bit of prediction about the specifications of the behavior (key=value) and allow host bugs to cause bugs in Zig code that uses them.
  2. recognize and work around WASI host bugs & undocumented behavior.

Given that WASI is so brand new, my preference is (1). That means that we recommend Debug or ReleaseSafe builds for now - due to upstream/downstream bugs - and then work with the third party projects to get their bugs fixed / documentation clarified. We can point to the zig standard library and say "this is how we want to implement it, it will work like this if you fix the bug / clarify the docs".

std/os.zig Outdated
for (environ) |env| {
if (env) |ptr| {
var line_i: usize = 0;
while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this, but you might consider using std.mem.separate

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combining this and the other comment about assert, you could do this:

const line = std.mem.toSlice(u8, ptr);
var it = std.mem.separate(line, "=");
const key = it.next().?;
const value = it.next().?;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. This also cleanly handles wasmer.

@andrewrk andrewrk merged commit 79a139a into ziglang:master Apr 30, 2019
@shritesh shritesh deleted the wasi_env branch May 1, 2019 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants